package com.zw.pm.module.project.change;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.gen.entity.Project;
import com.zw.pm.module.project.build.ProjectModel;
import com.zw.pm.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 项目变更 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/project/change")
public class ChangeController extends BaseController {

    @Autowired
    private ChangeService changeService;
    @Autowired
    private LogService logService;

    /**
     * 项目页面
     * 
     * @return 页面
     */
    @GetMapping
    @RequiresPermissions({"project:change"})
    public String project(Model model) {
        model.addAttribute("taskTemps", changeService.findTaskTemps());
        model.addAttribute("factories", changeService.findFactories());
        return "project/change/page";
    }

    /**
     * 项目列表
     * 
     * @param project 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"project:change"})
    public JsonResult list(ProjectModel project) {
        startPage();
        List<ProjectModel> list = changeService.findProject(project);
        return jsonResult(list);
    }

    /**
     * 根据ID查询
     *
     * @param id id
     * @return Project
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"project:change"})
    public ProjectModel id(Integer id) {
        return changeService.getById(id);
    }

    /**
     * 变更项目
     *
     * @param project project
     * @param factoryIds 生产厂ID
     * @return json
     */
    @ResponseBody
    @PostMapping("/apply")
    @RequiresPermissions({"project:change"})
    public JsonResult apply(Project project, @RequestParam("factoryIds") Integer[] factoryIds,@RequestParam("taskIds") Integer[] taskIds) {
        logService.saveLog("变更项目", project.getName());
        return jsonResult(changeService.change(project, factoryIds, taskIds));
    }

}
